package com.jinlia.show.modules.base.wxapp.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.model.form.GoodsForm;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.system.service.GoodsMapperService;
import com.jinlia.show.modules.base.wxapp.entity.FavouriteGoods;
import com.jinlia.show.modules.base.wxapp.mapper.FavouriteGoodsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FavouriteGoodsMapperService extends ServiceImpl<FavouriteGoodsMapper, FavouriteGoods> {
    final GoodsMapperService goodsMapperService;
    public Boolean addSwitch(Long userId, Long goodsId) {
        FavouriteGoods oneFavouriteGoods = this.lambdaQuery()
                .eq(FavouriteGoods::getGoodsId, goodsId)
                .eq(FavouriteGoods::getUserId, userId)
                .one();

        if (Objects.isNull(oneFavouriteGoods)) {
            FavouriteGoods favouriteGoods = new FavouriteGoods();
            favouriteGoods.setGoodsId(goodsId);
            favouriteGoods.setUserId(userId);
            this.save(favouriteGoods);
            return Boolean.TRUE;
        } else {
            this.removeById(oneFavouriteGoods.getId());
            return Boolean.FALSE;
        }
    }


    public List<GoodsPO> getLove(Long userId) {
        List<FavouriteGoods> list = this.lambdaQuery()
                .eq(FavouriteGoods::getUserId, userId)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return Arrays.asList();
        }
        List<Long> loveCollection = list.stream().map(FavouriteGoods::getGoodsId).collect(Collectors.toList());
        GoodsForm goodsForm = new GoodsForm();
        goodsForm.setGoodsIds(loveCollection);
        List<GoodsPO> voList = goodsMapperService.getBaseMapper().getVOList(goodsForm);
        return voList;
    }

    public Boolean isLove(Long userId,Long goodsId) {
        FavouriteGoods oneFavouriteGoods = this.lambdaQuery()
                .eq(FavouriteGoods::getGoodsId, goodsId)
                .eq(FavouriteGoods::getUserId, userId)
                .one();

        if (Objects.nonNull(oneFavouriteGoods)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}
